//
//  HRRegisterViewController.m
//  haierapp
//
//  Created by zhang on 14-8-6.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRRegisterViewController.h"

#import "UsersBll.h"


@interface HRRegisterViewController (){
    BOOL _registerFlag;//是否已发送注册（判断是否使用重新发送验证码接口）
}

@property (weak, nonatomic) IBOutlet UIButton *vervifyButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;


@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UITextField *comfirmPwdField;
@property (weak, nonatomic) IBOutlet UITextField *verifyCode;

@end

@implementation HRRegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIImage *verifyImage=[[UIImage imageNamed:@"blue_btn1"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    UIImage *registerImage=[[UIImage imageNamed:@"btn"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    [_vervifyButton setBackgroundImage:verifyImage forState:UIControlStateNormal];
    
    [_registerButton setBackgroundImage:registerImage forState:UIControlStateNormal];
    
    self.title=@"注册";
    
    _registerFlag=NO;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods

//获取验证码
- (IBAction)onTapGetVerifyCode:(id)sender {
   
    NSString *phone=_phoneField.text;
    NSString *password=_pwdField.text;
    NSString *confirmPassword = _comfirmPwdField.text;
    
    BOOL isVerify=[self VerifyInformation];
    
    if (isVerify) {
        if (_registerFlag) {
            //重新发送验证码
            [self showLoadingHud:kLoading];
            [HRInterfaceManager activityCodeInRegisterWithUserName:phone withMobile:phone completion:^(BOOL isSucceed, NSString *message, id data) {
                
                [self hideLoadingHud];
                
                if (isSucceed) {
                    [self showSuccessHud:@"请于10分钟内完成注册"];
                }else{
                    [self showTextHUD:message];
                }
                
            }];
        }else{
            //获取验证码
            [self showLoadingHud:kLoading];
            [HRInterfaceManager registerWithUserName:phone withPassword:password withMobile:phone completion:^(BOOL isSucceed, NSString *message, id data) {
                [self hideLoadingHud];
                
                if (isSucceed) {
                    _registerFlag=YES;
                    [self showSuccessHud:@"验证码发送成功，请于10分中内操作"];
        
                }else{
                    [self showTextHUD:message];
                }
                
            }];
        }
    }
    
}

//注册
- (IBAction)onTapRegister:(id)sender {
    NSString *phone=_phoneField.text;
    NSString *password=_pwdField.text;
    
    BOOL isVerify=[self VerifyInformation];
    
    if (isVerify) {
        NSString *code=_verifyCode.text;
        
        if ([code isEqualToString:@""]) {
            [self showTextHUD:@"请输入激活码"];
            return;
        }else{
            [self showLoadingHud:kLoading];
            
            [HRInterfaceManager registerActivityWithUserName:phone withActivityCode:code completion:^(BOOL isSucceed, NSString *message, id data) {
                [self hideLoadingHud];
                
                if (isSucceed) {
                    [self showSuccessHud:@"注册成功！"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(HUD_DELAY_HIDE_SECOND * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                }else{
                    [self showTextHUD:message];
                }
            }];
        }
    }
    
}

//验证输入的信息
-(BOOL)VerifyInformation{
    BOOL flag=YES;
    
    NSString *phone=_phoneField.text;
    NSString *password=_pwdField.text;
    NSString *confirmPassword=_comfirmPwdField.text;
    
    if ([phone isEqualToString:@""]) {
        [self showTextHUD:@" 手机号码不能为空"];
        flag=NO;
    }else if ([password isEqualToString:@""]){
        [self showTextHUD:@"密码不能为空"];
        flag=NO;
    }else if (![password isEqualToString:confirmPassword]) {
        [self showTextHUD:@"你输入的密码不一致"];
        flag=NO;
    }
    
    return flag;
}

#pragma mark - Text Field Delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
